var host = "http://www.owin.cc/"
var ajaxUrlObj = {
  'GET_INDEX_BANNER':host+"index_get_banner",
  'GET_COMPANY_INFO':host+"getCompanyInfo",
  'GET_CASES_LIST':host+"getCasesListByWeb",
  'GET_PROJECT_BANNER':host+"get_project_banner",
  'GET_ABOUT_INFO':host+"getAboutInfo",
  'GET_NEWS_LIST':host+"getInformationList",
  'POST_MESSAGE':host+"addCustomerMessage",
  'GET_NEW_DETAIL':host+"getInformation",
  'GET_PRODUCT_LIST':host+"getProductList",
  'GET_CASES_DETAIL':host+"getCases",
  'GET_POST_CODE':host+"getImageCode",
  'GET_SEO':host+"getSeo",
  'GET_PRODUCT_DETAIL':host+"getProduct",
  'GET_SEARCH_LIST':host+"get_list_by_keyword",
  'GET_TREE':host+"getTree"
}
var cacheStorage = JSON.parse(localStorage.getItem("cacheStorage"))
if (!cacheStorage) {
  cacheStorage = {}
}
function fetchApi (config) {
  var type = config.type;
  var apiKey = config.apiKey;
  var postData = config.postData;
  var successCb = config.successCb;
  var errorCb = config.errorCb;
  var nowDate = new Date().getTime()
  if (!cacheStorage[apiKey]) {
    cacheStorage[apiKey] = {}
  }
  var cacheData = cacheStorage[apiKey]
  if (cacheData.date && nowDate - cacheData.date < 2*60*60*1000){
    successCb && successCb(cacheData.data)
  } else {
    $.ajax({
      type: type,
      url: ajaxUrlObj[apiKey],
      data: postData,
      success: function(rep){
        if (typeof rep === 'string') {
          rep = JSON.parse(rep)
        }
        if (rep.code == 1){
          if (rep.data === 'null') return
          var data = rep.data ? rep.data : rep
          data = typeof data === 'string' ? JSON.parse(data) : data
          if(data.rows&&typeof data.rows === 'string'){
            data.rows = JSON.parse(data.rows)
          }
          cacheData.data = data
          cacheData.date = nowDate
          localStorage.setItem("cacheStorage", JSON.stringify(cacheStorage))
          successCb && successCb(data)
        }
      },
      error: function(error){
        console.log(error)
        errorCb && errorCb(error)
      }
    });
  }
}
function getProductNavBar(){
  fetchApi({
    type:"GET",
    apiKey:'GET_TREE',
    successCb:function(data){
      var str = getProductNavBarDom(data)
      $("#navbarProduct").html(str)
      var tempNav = []
      for(var i =0,l=allProductNav.length;i<l;i++){
        tempNav[allProductNav[i].id] = allProductNav[i]
      }
      allProductNav = tempNav
      localStorage.setItem("productNav", JSON.stringify(allProductNav))
    }
  })
}
var allProductNav = []
function getProductNavBarDom (data) {
  var str = ''
  allProductNav = allProductNav.concat(data)
  for(var i=0,l=data.length;i<l;i++){
    if(data[i].children &&data[i].children.length>0){
      str += '<a class="menu-item" href="product.html?type='+data[i].id+'"><span >'+data[i].text+'</span><i class="iconfont"></i><div class="menu-box">'
      str+=getProductNavBarDom(data[i].children)
      str+= '</div></a>'
    }else{
      str += '<a class="menu-item" href="product.html?type='+data[i].id+'"><span >'+data[i].text+'</span></a>'
    }
  }
  return str
}
function setNavBar () {
  var str
  if(sessionStorage.getItem("language") === "en"){
    str = '<div class="logo-box"> <a class="logo-link" target="_blank"> <img src="/img/logo.png" alt="" title="" width="100%" height="100%" /> </a> </div> <div class="nav-box"> <div class="e_link"> <a href="index.html" target="_self">HOME</a> </div> <div class="e_link"> <a href="aboutus.html" target="_self">ABOUT</a> </div> <div class="e_link"> <a class="e_link">PRODUCTS</a> <div class="menu-box" id="navbarProduct"> </div> </div> <div class="e_link"> <a href="projects.html" target="_self">CASES</a> </div> <div class="e_link"> <a href="news.html" target="_self">NEWS</a> </div> <div class="e_link"> <a href="contact.html" target="_self">CONTACT</a> </div> </div> <div class="search-box"> <div class="e_input p_input"> <input id="search_keyword" name="search_keyword" class="InputText" type="text" value="" placeholder="search"> </div> <div class="p_searchBtn"></div> </div><p class="lang-box"><a data-type="0">中文</a>|<a data-type="1" class="active">EN</a></p>'
  } else {
    str = '<div class="logo-box"> <a class="logo-link" target="_blank"> <img src="/img/logo.png" alt="" title="" width="100%" height="100%" /> </a> </div> <div class="nav-box"> <div class="e_link"> <a href="index.html" target="_self">首页</a> </div> <div class="e_link"> <a href="aboutus.html" target="_self">我们</a> </div> <div class="e_link"> <a class="e_link">产品</a> <div class="menu-box" id="navbarProduct"> </div> </div> <div class="e_link"> <a href="projects.html" target="_self">案例</a> </div> <div class="e_link"> <a href="news.html" target="_self">资讯</a> </div> <div class="e_link"> <a href="contact.html" target="_self">联系</a> </div> </div> <div class="search-box"> <div class="e_input p_input"> <input id="search_keyword" name="search_keyword" class="InputText" type="text" value="" placeholder="请输入搜索内容"> </div> <div class="p_searchBtn"></div> </div><p class="lang-box"><a class="active" data-type="0">中文</a>|<a data-type="1">EN</a></p>'
  }
  $("#publicNav").html(str)
  $(".search-box .p_searchBtn").on("click",function(){
    window.location.href = "search.html?keyword=" + encodeURI($("#search_keyword").val())
  })
}
function setFooter () {
  fetchApi({
    type:"GET",
    apiKey:'GET_COMPANY_INFO',
    successCb:function(data){
      var str = '<div class="footer-body"><p class="footer-left"><span>COPYRIGHT©2019&nbsp;'+data.companyName_zh+'&nbsp;备案号：粤<a href="http://www.miitbeian.gov.cn">ICP备16049531号</a>&nbsp;&nbsp;</span></p><div class="footer-right"><img src="/img/logo2.png" width="200px"></div></div>'
      setTimeout(function () {
        $("#publicFooter").css('top',$(document).height()+'px').html(str)
      }, 1500)
    }
  })
}
function getQueryString(name) { 
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
  var r = window.location.search.substr(1).match(reg); 
  if (r != null) return r[2]; 
  return null; 
}
function getSeoInfo (obj) {
  var id= obj.id;
  var name = obj.name;
  if(id) {
    return
  } else if(name) {
    fetchApi({
      type:"POST",
      apiKey:'GET_SEO',
      successCb:function(data){
        $("title").html(data.title)
        $('meta[name="keywords"]').attr("content",data.keyword)
        $('meta[name="description"]').attr("content",data.descs)
      }
    })
  }
}
if (window.screen.width <= 640 ) {
  window.location.href = 'http://www.owin.cc/mobile/index.html'
}
$(window).ready(function(){
  setNavBar()
  setFooter()
  getProductNavBar()
  var seoObj = {}
  var id = getQueryString('id')
  if (id) {
    seoObj.id = id
  } else {
    seoObj.name="index"
  }
  getSeoInfo(seoObj)
  $("body").on("click", ".lang-box a",function(){
    var type = $(this).attr("data-type")
    sessionStorage.setItem("language", type);
    loadTranslate(type == '1' ?  'en': 'ch')
  })
})

function loadTranslate (type) {
  $.ajax({
    type: 'GET',
    url: host + 'changeLanguage?language=' + type,
    success: function(rep){
      if (typeof rep === 'string') {
        rep = JSON.parse(rep)
      }
      if (rep.code == 1){
        sessionStorage.setItem("language", type);
        localStorage.removeItem("cacheStorage")
        localStorage.removeItem("productNav")
        window.location.reload()
      }
    },
    error: function(error){
      console.log(error)
    }
  });
}